package agent.moves;

import gui.threed.LabyrinthModel;

import javax.media.j3d.Transform3D;
import javax.vecmath.Vector3f;

import labyrinth.Position;

/**
 * A move of an agent in a specific direction
 * 
 * @author Johannes Kulick
 * @version 0.1
 * @licence GNU Lesser Genaral Public Licence
 */
public class DirectionMove implements IAgentMove {

	/**
	 * The four directions you can go to
	 */
	public final static int DOWN = 0;
	public final static int LEFT = 1;
	public final static int UP = 2;
	public final static int RIGHT = 3;
	public final static int STOP = 4;
	
	private int mDirection;
	
	/**
	 * Standard c'tor, which sets the direction to go
	 * @param direction The direction to go
	 * @throws InvalidDirectionException If you pass an int, which is not a valid direction
	 */
	public DirectionMove(int direction) throws InvalidDirectionException {
		setDirection(direction);
	}
	/**
	 * Sets the direction to go
	 * @param direction The direction to go
	 * @throws InvalidDirectionException If you pass an int, which is not a valid direction
	 */
	public void setDirection(int direction) throws InvalidDirectionException 
	{
		if(direction < 0 || direction > 4)
			throw new InvalidDirectionException(direction);
		else
			mDirection = direction;
	}

	public Position getNextPosition() {
		if(mDirection == STOP) {
			return new Position();
		}
		else {
			Position next = new Position();
			next.x = (mDirection % 2) * (int) Math.signum(mDirection - 2);
			next.y = (mDirection % 2 - 1) * (int) Math.signum(mDirection - 1);
			return next;
		}
	}
	public Transform3D getTransformation() {
		if(mDirection == STOP) {
			return new Transform3D();
		}
		else {
			Transform3D result = new Transform3D();
			// move into the given directory with modulo-magic
			int x = (mDirection % 2) * (int) Math.signum(mDirection - 2);
			int y = (mDirection % 2 - 1) * (int) Math.signum(mDirection - 1);
			result.set(new Vector3f(x * LabyrinthModel.BLOCK_SIZE*2, 0, y * LabyrinthModel.BLOCK_SIZE*2));
			return result;
		}
	}

}
